#include "stdafx.h"

#include "ParticleHolder.h"
#include "game.h"

ParticleHolder::ParticleHolder( stringw effectType) 
	:m_isVisible(false)
{
	getGame()->CloneObjectEffectType(this,effectType );
	m_selfManagement = true;	
	//m_attachedNode = Application::GetDevice()->getSceneManager()->addEmptySceneNode();
	//Application::GetInstance()->GetGame()->GetWorld().SetObjectEffectType( m_attachedNode, particleSystem, debugName );
	//m_attachedNode->setVisible(false);	
}

ParticleHolder::~ParticleHolder()
{
	/*m_attachedNode->remove();
	m_attachedNode->removeAll();
	m_attachedNode->drop();*/
}

void ParticleHolder::AddChild(IFpsParticleSystemSceneNode* particleSystem)
{
	m_particleSystems.push_back(particleSystem);
}

void ParticleHolder::SetPosition(float x, float y,bool bIsNew)
{
	for (int i=0; i<m_particleSystems.size(); i++)
	{
		m_particleSystems[i]->SetPosition(x,y,bIsNew);
	}
}
void ParticleHolder::render()
{
	if(!m_isVisible)
		return;
	for (int i=0; i<m_particleSystems.size(); i++)
	{
		m_particleSystems[i]->render();
	}
}
void ParticleHolder::update(float time)
{
	if(!m_isVisible)
		return;
	for (int i=0; i<m_particleSystems.size(); i++)
	{
		m_particleSystems[i]->doParticleSystem(time);
	}
}

void ParticleHolder::SetVisible(bool isVisible)
{
	if( m_isVisible != isVisible )
	{
		m_isVisible = isVisible;
		//m_attachedNode->setVisible(isVisible);
		
		if( isVisible )
		{
			for (int i=0; i<m_particleSystems.size(); i++)
			{
				m_particleSystems[i]->Link();
			}

			Restart();
		}
		else
		{
			for (int i=0; i<m_particleSystems.size(); i++)
			{
				m_particleSystems[i]->FreeLink();
				SAFE_DELETE(m_particleSystems[i]);
				m_particleSystems[i] = NULL;
			}
			m_particleSystems.clear();
		}
	}
}

void ParticleHolder::Restart()
{
	m_Stopped = false;
	for (int i=0; i<m_particleSystems.size(); i++)
	{		
		m_particleSystems[i]->Restart();	
	}
}

void ParticleHolder::StopEmitter()
{
	m_Stopped = true;

	for (int i=0; i<m_particleSystems.size(); i++)
	{
		m_particleSystems[i]->StopEmitting();
	}
}


bool ParticleHolder::IsFinished()
{
	for (int i=0; i<m_particleSystems.size(); i++)
	{
		if(!m_particleSystems[i]->IsEnded())
		{
			return false;
		}
	}

	return true;
}

bool ParticleHolder::IsActive()
{
	for (int i=0; i<m_particleSystems.size(); i++)
	{
		if(m_particleSystems[i]->IsActive())
		{
			return true;
		}
	}

	return false;
}
//
//void ParticleHolder::SaveData(CWriteFile* stream)
//{
//	stream->Write(&m_position, sizeof(m_position));
//	stream->Write(&m_direction, sizeof(m_direction));
//
//	stream->WriteU8(m_isVisible);
//	stream->WriteU8(m_isManual);
//	stream->WriteU8(m_Stopped);
//}
//
//void ParticleHolder::LoadData(CReadFile* stream)
//{
//	stream->Read(&m_position, sizeof(m_position));
//	stream->Read(&m_direction, sizeof(m_direction));
//
//
//	SetVisible(stream->ReadU8());
//	SetManualEmitControl(stream->ReadU8());
//	if (stream->ReadU8())
//		StopEmitter();
//	else
//		Restart();
//}
//
//void ParticleHolder::Restart()
//{
//	m_Stopped = false;
//	const core::list<ISceneNode*>& list = m_attachedNode->getChildren();
//	core::list<ISceneNode*>::ConstIterator it = list.begin();
//
//	for (; it!=list.end(); ++it)
//	{
//		IFpsParticleSystemSceneNode * node = (IFpsParticleSystemSceneNode *)(*it);
//		node->Restart();
//		node->OnAnimate(((CSceneManager*)Application::GetDevice()->getSceneManager())->getSceneTime()/*irr::os::Timer::getTime()*/); //cgm : force an update
//	}
//}
//
//void ParticleHolder::SetPosition(Vector3D position)
//{
//	m_position = position;
//	m_attachedNode->setPosition(position);
//	m_attachedNode->updateAbsolutePosition(true);
//}
//
//void ParticleHolder::SetVisible(bool isVisible)
//{
//	if( m_isVisible != isVisible )
//	{
//		m_isVisible = isVisible;
//		m_attachedNode->setVisible(isVisible);
//		
//		if( isVisible )
//		{
//			const core::list<ISceneNode*>& list = m_attachedNode->getChildren();
//			core::list<ISceneNode*>::ConstIterator it = list.begin();
//
//			for (; it!=list.end(); ++it)
//			{
//				((IFpsParticleSystemSceneNode *)(*it))->Link();
//			}
//
//			Restart();
//		}
//		else
//		{
//			const core::list<ISceneNode*>& list = m_attachedNode->getChildren();
//			core::list<ISceneNode*>::ConstIterator it = list.begin();
//
//			for (; it!=list.end(); ++it)
//			{
//				((IFpsParticleSystemSceneNode *)(*it))->FreeLink();
//			}
//		}
//	}
//}
//
//void ParticleHolder::SetDirection(Vector3D direction)
//{
//	m_direction = direction;
//	const core::list<ISceneNode*>& list = m_attachedNode->getChildren();
//	core::list<ISceneNode*>::ConstIterator it = list.begin();
//
//	for (; it!=list.end(); ++it)
//	{
//		IFpsParticleSystemSceneNode * node = (IFpsParticleSystemSceneNode *)(*it);
//		IFpsParticleEmitter* emitter = node->getEmitter();
//		
//		if(emitter)
//		{
//			emitter->setDirection(direction);
//		}
//	}
//}
//
//bool ParticleHolder::IsFinished()
//{
//	const core::list<ISceneNode*>& list = m_attachedNode->getChildren();
//	core::list<ISceneNode*>::ConstIterator it = list.begin();
//
//	for (; it!=list.end(); ++it)
//	{
//		IFpsParticleSystemSceneNode * node = (IFpsParticleSystemSceneNode *)(*it);
//		
//		if(!node->IsEnded())
//		{
//			return false;
//		}
//	}
//
//	return true;
//}
//
//bool ParticleHolder::IsActive()
//{
//	const core::list<ISceneNode*>& list = m_attachedNode->getChildren();
//	core::list<ISceneNode*>::ConstIterator it = list.begin();
//
//	for (; it!=list.end(); ++it)
//	{
//		IFpsParticleSystemSceneNode * node = (IFpsParticleSystemSceneNode *)(*it);
//		
//		if(node->IsActive())
//		{
//			return true;
//		}
//	}
//
//	return false;
//}
//
//void ParticleHolder::StopEmitter()
//{
//	m_Stopped = true;
//	const core::list<ISceneNode*>& list = m_attachedNode->getChildren();
//	core::list<ISceneNode*>::ConstIterator it = list.begin();
//
//	for (; it!=list.end(); ++it)
//	{
//		IFpsParticleSystemSceneNode * node = (IFpsParticleSystemSceneNode *)(*it);
//		node->StopEmitting();
//	}
//}
//
//
//void ParticleHolder::EmitParticlesNow(int nbParticles)
//{
//	const core::list<ISceneNode*>& list = m_attachedNode->getChildren();
//	core::list<ISceneNode*>::ConstIterator it = list.begin();
//
//	for (; it!=list.end(); ++it)
//	{
//		IFpsParticleSystemSceneNode * node = (IFpsParticleSystemSceneNode *)(*it);
//		node->emitNow(nbParticles);
//	}
//}
//
//
//void ParticleHolder::SetManualEmitControl(bool isManual)
//{
//	m_isManual = isManual;
//	const core::list<ISceneNode*>& list = m_attachedNode->getChildren();
//	core::list<ISceneNode*>::ConstIterator it = list.begin();
//
//	for (; it!=list.end(); ++it)
//	{
//		IFpsParticleSystemSceneNode * node = (IFpsParticleSystemSceneNode *)(*it);
//		node->setManualEmitControl(isManual);
//	}
//}